home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Random / Commodore 64c / SOURCE / Serial.c < prev    next >
Text File  |  1994-02-13  |  4KB  |  186 lines

  1. /*
  2.     Commodore 64 Emulator v0.1      Earle F. Philhower III 
  3.     Copyright (C) 1993-4            (st916w9r@dunx1.ocs.drexel.edu)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "Flags.h"
  20. #include "Registers.h"
  21. #include "Memory.h"
  22. #include "Traps.h"
  23. #include "Serial.h"
  24. #include "Error.h"
  25.  
  26. typedef struct {
  27.     byte inuse  : 1;
  28.     byte isopen : 1;
  29.     byte (*getf)(byte *, int);
  30.     byte (*putf)(byte, int);
  31.     byte (*openf)(char *, int, int);
  32.     byte (*closef)(int);
  33.     void (*flushf)(int);
  34. } SerialType;
  35. static SerialType device[16];
  36.  
  37. #define BUFFERLEN 127
  38. static byte serialBuffer[BUFFERLEN];
  39.  
  40. static byte serialPtr, latestListen, trapListen, latestSaListen;
  41.  
  42. static void SerialSendByte(void), SerialRecieveByte(void), SerialListen(void),
  43.             SerialSaListen(void), FloppyReadyTrap(void);
  44. static byte SerialDummy(void);
  45.  
  46. static trap serialTrap[] = {
  47.     { 0xed40, {0x78, 0x20, 0x97}, SerialSendByte },
  48.     { 0xee13, {0x78, 0xa9, 0x00}, SerialRecieveByte },
  49.     { 0xed0e, {0x20, 0xa4, 0xf0}, SerialListen },
  50.     { 0xed36, {0x78, 0x20, 0x8e}, SerialSaListen },
  51.     { 0xeea9, {0xad, 0x00, 0xdd}, FloppyReadyTrap } };
  52.  
  53. void SerialInitialize(void)
  54. {
  55.     byte i;
  56.     SerialType *p;
  57.     
  58.     AddTrap(serialTrap[0]);
  59.     AddTrap(serialTrap[1]);
  60.     AddTrap(serialTrap[2]);
  61.     AddTrap(serialTrap[3]);
  62.     AddTrap(serialTrap[4]);
  63.  
  64.     for (i = 0; i < 16; i++) {
  65.         p=&device[i];
  66.         p->getf=(byte (*)(byte *, int ))SerialDummy;
  67.         p->putf=(byte (*)(byte, int ))SerialDummy;
  68.         p->openf=(byte (*)(char *, int , int ))SerialDummy;
  69.         p->closef=(byte (*)(int ))SerialDummy;
  70.         p->flushf=(void (*)(int ))NULL; }
  71. }
  72.  
  73. void AddSerialDevice(int devnum, byte (*getf)(byte *, int),
  74.     byte (*putf)(byte, int), byte (*openf)(char *, int, int),
  75.     byte (*closef)(int), void (*flushf)(int))
  76. {
  77.     SerialType *p;
  78.  
  79.     if ((devnum<0)||(devnum>15)) InternalError(kInvalidDeviceNumber);
  80.     p=&device[devnum];
  81.     if (p->inuse!=0) InternalError(kDeviceNumberInUse);
  82.  
  83.     p->getf = getf;
  84.     p->putf = putf;
  85.     p->openf = openf;
  86.     p->closef = closef;
  87.     p->flushf = flushf;
  88.     p->inuse = 1;
  89. }
  90.  
  91.  
  92. static void SerialSendByte(void)
  93. {
  94.     byte data;
  95.     SerialType *p;
  96.     extern void i60(void);
  97.  
  98.     data=RAM[0x95];
  99.     p=&device[latestListen&0x0f];
  100.  
  101.     if (p->isopen==0) {
  102.         if (serialPtr<BUFFERLEN) serialBuffer[serialPtr++] = data; }
  103.     else (*p->putf)(data, latestSaListen & 0x0f);
  104.     i60();    /* RTS */
  105. }
  106.  
  107. static void SerialRecieveByte(void)
  108. {
  109.     byte pp;
  110.     SerialType *p;
  111.     extern void i60(void);
  112.  
  113.     p=&device[latestListen&0x0f];
  114.  
  115.     RAM[0x90]=(*p->getf)(&pp, latestSaListen&0x0f);
  116.     RAM[0xa4]=a=pp;
  117.     flags &= ~CAR;
  118.     i60();    /* RTS */
  119. }
  120.  
  121. static void SerialListen(void)
  122. {
  123.     extern void i60(void);
  124.     
  125.     trapListen=a;
  126.     i60();    /* RTS */
  127. }
  128.  
  129. static void SerialSaListen(void)
  130. {
  131.     SerialType *p;
  132.     byte b, i;
  133.     extern void i60(void);
  134.  
  135.     b=RAM[0x95];
  136.     p=&device[latestListen&0x0f];
  137.  
  138.     if (b==0x3f)
  139.         switch (latestSaListen&0xf0) {
  140.             case 0xf0:
  141.                  if (p->isopen==0) {
  142.                     p->isopen=1;
  143.                     i=(*p->openf)((char *)serialBuffer, serialPtr, latestSaListen&0x0f);
  144.                     serialPtr=0;
  145.                     RAM[0x90]=i;
  146.                     if (i) {
  147.                         p->isopen=0;
  148.                         (*p->closef)(latestSaListen&0x0f); } }
  149.                 if (p->flushf) (*p->flushf)(latestSaListen&0x0f);
  150.                 break;
  151.             case 0x60:
  152.                 if (p->isopen==0) {
  153.                     p->isopen=1;
  154.                     (*p->openf)(NULL, 0, latestSaListen&0x0f);
  155.                     for (i=0; i<serialPtr; i++)
  156.                         (*p->putf)(serialBuffer[i], latestSaListen&0x0f);
  157.                     serialPtr=0; }
  158.                 if (p->flushf) (*p->flushf)(latestSaListen&0x0f);
  159.                 break;
  160.             case 0xe0:
  161.                 p->isopen=0;
  162.                 (*p->closef)(latestSaListen&0x0f);
  163.                 break;
  164.             default:
  165.                 DebugStr("\pUnimplemented Serial Command"); break;
  166.             }
  167.  
  168.     latestListen=trapListen;
  169.     latestSaListen=b;
  170.  
  171.     i60();    /* RTS */
  172. }
  173.  
  174. static void FloppyReadyTrap(void)
  175. {
  176.     extern void i60(void);
  177.     
  178.     a = 0x01;
  179.     flags &=(~NEG+ZER);
  180.     i60();    /* RTS */
  181. }
  182.  
  183.  
  184.  
  185. static byte SerialDummy(void) { return 2; }
  186.